<?php
// +----------------------------------------------------------------------
// | zhanshop-marketing / AdshowCallQueue.php    [ 2024/12/23 15:50 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2011~2024 zhangqiquan All rights reserved.
// +----------------------------------------------------------------------
// | Author: zhangqiquan <768617998@qq.com>
// +----------------------------------------------------------------------
declare (strict_types=1);

namespace app\library;

use Swoole\Coroutine\Channel;
use Swoole\Timer;
use zhanshop\App;
use zhanshop\Error;

class AdshowCallQueue
{
    protected $database = "";
    protected $channel;
    public function __construct()
    {
        $capacity = App::env()->get("AD_QUEUE_CAPACITY", 500);
        $this->channel = new Channel($capacity);

        Timer::tick(3000, function () {
            try {
                if (!$this->channel->isEmpty()){
                    $this->write();
                }
            }catch (\Throwable $e){
                Error::exceptionHandler($e);
            }
        });
    }

    /**
     * 写入广告队列
     * @param string $appId
     * @param string $platform
     * @param string $event
     * @param string $accountId
     * @param string $aid
     * @param string $oaid
     * @param string $imei
     * @param string $androidId
     * @param string $idfa
     * @param string $ipv4
     * @param string $ipv6
     * @param string $ua
     * @param string $os
     * @param string $mac
     * @param string $imsi
     * @param string $phone
     * @param array $params
     * @return void
     */
    public function push(string $appId, string $platform, string $event, string $accountId, string $planId, string $adid, string $creativeId,  string $adName, string  $oaid, string $imei, string $androidId, string $idfa, string $iddi, string $ipv4, string $ipv6, string $ua, string $os, string $mac, array $params)
    {
        $this->channel->push([
            'app' => $appId,
            'platform' => $platform,
            'event' => $event,
            'account_id' => $accountId,
            'plan_id' => $planId,
            'adid' => $adid,
            'creative_id' => $creativeId,
            'adname' => $adName,
            'oaid' => $oaid,
            'imei' => $imei,
            'android_id' => $androidId,
            'idfa' => $idfa,
            'iddi' => $iddi,
            'ipv4' => $ipv4,
            'ipv6' => $ipv6,
            'ua' => $ua,
            'os' => $os,
            'mac' => $mac,
            'params' => json_encode($params),
            'not_upload' => $params['not_upload'] ?? 0,
        ]);
        if($this->channel->isFull()){
            $this->write();
        }
    }

    /**
     * 消费广告队列
     * @return void
     */
    public function write()
    {
        $time = time();
        $insertAll = [];
        while ($row = $this->channel->pop(0.001)){
            $row['create_time'] = $time;
            $insertAll[] = $row;
        }
        App::database()->table($this->database.'ad_record')->insertAll($insertAll);
    }
}